#define _CRT_SECURE_NO_WARNINGS １
#include<iostream>
#include<graphics.h>
#include<mmsystem.h>//多媒体设备接口
#pragma comment(lib,"winmm.lib")//预编译制指令
#define FOOD_NUM 200
#define WIN_W 1024
#define WIN_H 640
using namespace std;
struct Ball
{
	int x;
	int y;
	COLORREF color;
	int r;
	bool flag;//食物是否存活
};

struct Ball food[FOOD_NUM];
struct Ball hero;
void gameInit()
{
	//播放
	//打开
	mciSendString("open ./BallBGM.mp3", 0, 0, 0);
	mciSendString("play ./BallBGM.mp3", 0, 0, 0);
	for (int i = 0; i < FOOD_NUM; i++)
	{
		food[i].x = rand()%WIN_W;//[0-1024]
	    food[i].y = rand() % WIN_H;
		food[i].r = rand() % 5 + 1;
		food[i].flag = true;
		food[i].color = RGB(rand()%256,rand()%256,rand()%256);//三原色红蓝绿
	}
	hero.x= rand() % WIN_W;
	hero.y = rand() % WIN_H;
	hero.flag = true;
	hero.r = 10;
	hero.color = RED;
	settextcolor(BLACK);
	setbkmode(TRANSPARENT);
	settextstyle(20, 0, "楷体");
	char pname[] = "TanzeroASDFGHJFKDJLKDSJ";
	int twidth = textwidth(pname) / 2;//计算文字宽度为了居中显示在玩家中央
	outtextxy(hero.x - twidth, hero.y, pname);
	//有字符集问题
	/*settextcolor(RED);
	setbkmode(TRANSPARENT);
	
	
	settextstyle(20, 0, "黑体");
	outtextxy(hero.x-10, hero.y, "Tanzero");*/
	
}
void gamedraw()
{
	cleardevice();
	for (int i = 0; i < FOOD_NUM; i++)
	{
		if (food[i].flag==true) {
			setfillcolor(food[i].color);
			solidcircle(food[i].x, food[i].y, food[i].r);
		}
	}
	//绘制玩家
	
	setfillcolor(hero.color);
	solidcircle(hero.x, hero.y, hero.r);
}
//玩家移动
void heromove(int speed)
{
	//根据键盘的按键来判断_getchar getch() pass 一点不
	if (GetAsyncKeyState(VK_UP)&0x8000)
	{
		hero.y -= speed;
	}
    if(GetAsyncKeyState(VK_DOWN)&0x8000)
	{
		hero.y += speed;
	}
	 if (GetAsyncKeyState(VK_LEFT)&0x8000)
	{
		hero.x -= speed;
	}
	 if (GetAsyncKeyState(VK_RIGHT)&0x8000)
	{
		hero.x += speed;
	}

}
double distance(struct Ball b1, struct Ball b2)
{
	return sqrt((double)(b1.x - b2.x) * (b1.x - b2.x) + (b1.y - b2.y) * (b1.y - b2.y));
}
//吃食物
void eatfood()
{
	for (int i = 0; i < FOOD_NUM; i++) {
		if (distance(hero, food[i])<hero.r&&food[i].flag==true)
		{
			food[i].flag = false;
			hero.r += food[i].r/5;
		}
		if (!food[i].flag)
		{
			food[i].x = rand() % WIN_W;//[0-1024]
			food[i].y = rand() % WIN_H;
			food[i].r = rand() % 5 + 1;
			food[i].flag = true;
			food[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);//三原色红蓝绿

		}
	}

}
int main()
{
	//创建一个窗口
	initgraph(1024, 640,1);/*SHWOCONSOLE*/
	//玩家，敌人，食物，
	/*circle(50, 50, 20);*///空心圆
	/*setfillcolor(YELLOW);
	solidcircle(100, 50, 20);
	getchar(); */   //防止窗口闪退

  //设置背景颜色
	setbkcolor(WHITE);
	/*cleardevice();*/
	gameInit();
	while (1) {
		//双缓冲绘图
		BeginBatchDraw();
		gamedraw();
		EndBatchDraw();
		heromove(3);
		Sleep(10);
		eatfood();
	}
	getchar();//防止窗口闪退
	/*cleardevice();*/
	return 0;
}